package com.liu.io.file.pdf;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfPTable;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 导出PDF实现类
 * @author liubo
 */
public class RequestGeneratePdf extends GeneratePdf {

    PdfUtil pdfUtil = new PdfUtil();

    /**
     * 最大宽度
     */
    private int maxWidth = 550;

    /**
     * @param pdfData
     * @param fileName abc
     * @return
     */
    @Override
    public String createPDF(List<List<String>> pdfData, String fileName) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = df.format(new Date());
//        String fileDirectory = CommonHelper.getSysProperty("file.upload.directory");
        String fileDirectory = "file.upload.directory";
        if (fileDirectory.lastIndexOf("/") < fileDirectory.length() - 1) {
            fileDirectory += File.separator;
        }
        String filePath = "spt/pdf/" + fileName + "_" + date + ".pdf";

        PdfReport pdfReport = new PdfReport();
        pdfReport.createNewFile(fileDirectory + filePath, new RequestGeneratePdf(), pdfData, false);
        return filePath;
    }


    /**
     * 生成PDF文件
     *
     * @param document
     * @throws Exception
     */
    @Override
    public void generatePdf(Document document, List<List<String>> pdfData) throws Exception {
        document.add(createTable(pdfData));
    }

    public PdfPTable createTable(List<List<String>> data) {
        Font headfont = new Font(Font.COURIER, 10, Font.BOLD);
        Font keyfont = new Font(Font.COURIER, 8, Font.BOLD);
        Font textfont = new Font(Font.COURIER, 8, Font.NORMAL);


//        PdfPTable table = pdfUtil.createTable(maxWidth, new float[]{0.08f, 0.1f, 0.12f, 0.13f, 0.12f, 0.13f, 0.1f, 0.1f, 0.12f});
        PdfPTable table = pdfUtil.createTable(maxWidth, new float[]{0.25f, 0.25f, 0.2f, 0.15f, 0.15f});
        int cnt = 0;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).size() == 1) {
                table.addCell(pdfUtil.createCell(data.get(i).get(0), headfont, Element.ALIGN_CENTER, 5));
            }
            else if (data.get(i).size() == 2) {
                table.addCell(pdfUtil.createCell(data.get(i).get(0), keyfont, Element.ALIGN_LEFT, 1));
                table.addCell(pdfUtil.createCell(data.get(i).get(1), textfont, Element.ALIGN_LEFT, 4));
            }
            else {
                for (int j = 0; j < data.get(i).size(); j++) {
                    if (cnt == 0) {
                        table.addCell(pdfUtil.createCell(data.get(i).get(j), keyfont, Element.ALIGN_CENTER));
                    }
                    else {
                        table.addCell(pdfUtil.createCell(data.get(i).get(j), textfont, Element.ALIGN_CENTER));
                    }
                }
                cnt++;
            }
        }
        table.addCell(pdfUtil.createCell("Requestor Signature:", keyfont, Element.ALIGN_LEFT, 2));
        table.addCell(pdfUtil.createCell("Warehouse Administrator:", keyfont, Element.ALIGN_LEFT, 3));
        table.addCell(pdfUtil.createCell("Date:", keyfont, Element.ALIGN_LEFT, 2));
        table.addCell(pdfUtil.createCell("Date:", keyfont, Element.ALIGN_LEFT, 3));
        return table;
    }

    public List<List<String>> getPdfData(Map<String, String> requestOrder, List<HashMap<String, String>> requestOrderList, String title) {
        List<List<String>> data = new ArrayList<>();
        List<String> list = new ArrayList<>();

        //表格标题
        list.add(title);
        data.add(list);

        //仓库信息
        list = new ArrayList<>();
        list.add("Source Warehouse");
        list.add(requestOrder.get("SOURCE_WAREHOUSE_NAME"));
        data.add(list);
        list = new ArrayList<>();
        list.add("Target Warehouse");
        list.add(requestOrder.get("OBJ_WAREHOUSE_NAME"));
        data.add(list);
        list = new ArrayList<>();
        list.add("Requestor");
        list.add(requestOrder.get("REQUESTOR"));
        data.add(list);
        list = new ArrayList<>();
        list.add("Create Date");
        list.add(requestOrder.get("CREATE_DATE"));
        data.add(list);
        list = new ArrayList<>();
        list.add("Request Notes");
        list.add(requestOrder.get("REQUEST_NOTES"));
        data.add(list);

        //清单表格头
        list = new ArrayList<>();
        list.add("Part No");
        list.add("Part Name");
        list.add("Material No");
        list.add("Request Count");
        list.add("Actual Count");
//        list.add("Spare Part No");
//        list.add("Vendor ID");
//        list.add("Band Name");
//        list.add("Instance Seq");
        data.add(list);

        //清单表格数据
        for (int i = 0; i < requestOrderList.size(); i++) {
            Map<String, String> map = requestOrderList.get(i);
            list = new ArrayList<>();
            list.add(map.get("MODEL_CODE"));
            list.add(map.get("MODEL_NAME"));
            list.add(map.get("MATERIAL_NO"));
            list.add(map.get("TOTAL"));
            list.add(map.get("ACTUAL_CNT"));
//            list.add(map.get("SPARE_MODEL_ID"));
//            list.add(map.get("VENDOR_ID"));
//            list.add(map.get("BAND_NAME"));
//            list.add(map.get("INSTANCE_SEQ"));
            data.add(list);
        }
        return data;
    }

    @Override
    public List<List<String>> getPdfData(Map<String, Object> pdfData, String pdfTitle) {
        Map<String, String> requestOrder = (Map<String, String>) pdfData.get("requestOrder");
        List<HashMap<String, String>> requestOrderList = (List<HashMap<String, String>>) pdfData.get("requestOrderList");
        return getPdfData(requestOrder, requestOrderList, pdfTitle);
    }
}
